import 'package:flutter/cupertino.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:yuellyfactory/eventbus/EventBusUtil.dart';
import 'package:yuellyfactory/eventbus/EventRefreshSupplier.dart';
import 'package:yuellyfactory/views/dto/Supplier.dart';

import '../../util/BaseRequest.dart';
import '../../util/ToastUtil.dart';
import '../../util/ViewUtil.dart';

class SupplierEditVC extends StatefulWidget {
  final VoidCallback callback;
  final Supplier supplier;
  const SupplierEditVC({super.key, required this.callback, required this.supplier});

  @override
  State<SupplierEditVC> createState() => _SupplierEditVCState();
}

class _SupplierEditVCState extends State<SupplierEditVC> {

  final formKey = GlobalKey<ShadFormState>();

  @override
  Widget build(BuildContext context) {
    return ShadSheet(

      onClosing: (){
        debugPrint("关闭回调");
        widget.callback();
      },
      constraints: const BoxConstraints(minWidth: 800,maxWidth: 800),
      title: Row(
        children: [
          const Text('供应商编辑'),
          const SizedBox(width: 50,),
          ViewUtil.doneIcon("保存", (){
            submit();
          })
        ],
      ),
      child: ShadForm(
          key: formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: [

              Row(
                children: [
                  Expanded(child: ShadInputFormField(
                    id: 'company',
                    initialValue: widget.supplier.company,
                    label: const Text('公司名称'),
                    placeholder: const Text('请输入公司名称'),
                    validator: (v) {
                      if (v.isEmpty) {
                        return '请输入公司名称';
                      }
                      return null;
                    },
                  ),),
                  SizedBox(width: ViewUtil.screenWidth(10),),
                  Expanded(child: ShadInputFormField(
                    id: 'name',
                    initialValue: widget.supplier.name,
                    label: const Text('联系人'),
                    placeholder: const Text('请输入联系人'),
                    validator: (v) {
                      if (v.isEmpty) {
                        return '请输入联系人';
                      }
                      return null;
                    },
                  ),),
                ],
              ),


              const SizedBox(height: 10),

              Row(
                children: [
                  Expanded(child: ShadInputFormField(
                    id: 'cellphone',
                    initialValue: widget.supplier.cellphone,
                    label: const Text('联系电话'),
                    placeholder: const Text('请输入联系电话'),
                    validator: (v) {
                      if (v.isEmpty) {
                        return '请输入联系电话';
                      }
                      return null;
                    },
                  ),),
                  SizedBox(width: ViewUtil.screenWidth(10),),
                  Expanded(child: Container(),),
                ],
              ),


            ],
          )),
    );
  }


  void submit(){
    if (formKey.currentState!.saveAndValidate()) {
      var values= formKey.currentState!.value;
      var name=values["name"];
      var company=values["company"];
      var cellphone=values["cellphone"];
      var parm={
        "name":name,
        "company":company,
        "cellphone":cellphone,
        "id":widget.supplier.id,
      };
      BaseRequest.postHttp("supplier/edit",parameters: parm, canTip: true, context: context, onSuccess: (data){

        bool result=BaseRequest.chekResponse(data, true);
        if(result){
          ToastUtil.showToast("编辑成功");
          eventBus.fire(EventRefreshSupplier());
        }
        Navigator.pop(context);
      }, onError: (error){

      });


    }
  }
}
